package cn.sskxyz.mongodb.compress;

import java.util.List;

import com.mongodb.MongoNamespace;
import com.mongodb.ServerAddress;
import com.mongodb.ServerCursor;
import com.mongodb.client.MongoCursor;

public class CompressBatchCursor<T> implements MongoCursor<T> {

	private MongoCursor<T> delegated;
	
	private MongoNamespace namespace;
	
	private List<CompressKey> compressKeys;
	
	private CompressProcessor compressProcessor;

	public CompressBatchCursor(MongoCursor<T> delegated, MongoNamespace namespace, 
			List<CompressKey> compressKeys, CompressProcessor compressProcessor) {
		this.delegated = delegated;
		this.namespace = namespace;
		this.compressKeys = compressKeys;
		this.compressProcessor = compressProcessor;
	}

	@Override
	public void close() {
		this.delegated.close();
	}

	@Override
	public boolean hasNext() {
		return this.delegated.hasNext();
	}

	@Override
	public T next() {
		T doc = this.delegated.next();
		compressProcessor.decompress(doc, compressKeys, namespace);
		return doc;
	}

	@Override
	public T tryNext() {
		T doc = this.delegated.tryNext();
		compressProcessor.decompress(doc, compressKeys, namespace);
		return doc;
	}

	@Override
	public ServerCursor getServerCursor() {
		return this.delegated.getServerCursor();
	}

	@Override
	public ServerAddress getServerAddress() {
		return this.delegated.getServerAddress();
	}

	@Override
	public int available() {
		return this.delegated.available();
	}
}
